﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.Xml.Linq;
using System.Linq;
using System.IO;

namespace ActivityTracker.Library
{
    public class Maps
    {
        #region Global

        IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
        IsolatedStorageFileStream isoStream;
        XDocument xdoc;

        #endregion

        #region Methods

        public bool IsMapVisible(string actId)
        {
            bool isMapVisibility = false;
            try
            {
                if (isoStore.FileExists("ActivityTracker/MyActivities.xml"))
                {
                    isoStream = new IsolatedStorageFileStream("ActivityTracker/MyActivities.xml", FileMode.Open, isoStore);
                    xdoc = XDocument.Load(isoStream);
                    isoStream.Close();
                    var activities = from f in xdoc.Descendants("activity")
                                     where f.Attribute("id").Value == actId
                                     select f;
                    foreach (XElement mapVisible in activities)
                    {
                        isMapVisibility = Convert.ToBoolean(mapVisible.Element("locationtracking").Value);
                    }
                }
            }
            catch (Exception ex)
            {
                Exception exNew = new Exception("Exception in IsMapVisible Method In ActivityTracker.Library.Maps file\n\n" + ex.Message + " \n\n Stack Trace \n" + ex.StackTrace);
                exNew.Data.Add("actId_IsMapVisible", actId);
                throw exNew;
            }
            return isMapVisibility;
        }

        public void SaveLocations(string lat, string lon, string hisId, string dateTime, string pointNo)
        {
            try
            {
                string filename = "ActivityTracker/Location_" + hisId.ToString() + ".xml";
                if (!isoStore.DirectoryExists("ActivityTracker"))
                    isoStore.CreateDirectory("ActivityTracker");

                if (isoStore.FileExists(filename))
                {
                    isoStream = new IsolatedStorageFileStream(filename, FileMode.Open, isoStore);
                    xdoc = XDocument.Load(isoStream);
                    isoStream.Close();
                    var FindEle = (from f in xdoc.Descendants("Location")
                                   select f);
                    if (FindEle.Elements().Count() > 0)
                    {
                        xdoc.Root.Add(new XElement("point",
                                    new XAttribute("lattitude", lat),
                                    new XAttribute("longitude", lon),
                                    new XAttribute("datetime", dateTime),
                                    new XAttribute("pointno", pointNo)));

                        isoStream = new IsolatedStorageFileStream(filename, FileMode.Create, isoStore);
                        xdoc.Save(isoStream);
                        isoStream.Close();
                    }
                }
                else
                {
                    isoStream = new IsolatedStorageFileStream(filename, FileMode.Create, isoStore);
                    xdoc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"),
                                 new XElement("Location", new XAttribute("Id", hisId),
                                new XElement("point",
                                    new XAttribute("lattitude", lat),
                                    new XAttribute("longitude", lon),
                                    new XAttribute("datetime", dateTime),
                                    new XAttribute("pointno", pointNo))));
                    xdoc.Save(isoStream);
                    isoStream.Close();

                }
            }
            catch (Exception ex)
            {
                Exception exNew = new Exception("Exception in SaveLocations Method In ActivityTracker.Library.Maps file\n\n" + ex.Message + " \n\n Stack Trace \n" + ex.StackTrace);
                exNew.Data.Add("hisId_SaveGeoCodeLocations", hisId);
                exNew.Data.Add("lat_SaveGeoCodeLocations", lat);
                exNew.Data.Add("lon_SaveGeoCodeLocations", lon);
                exNew.Data.Add("dateTime_SaveGeoCodeLocations", dateTime);
                exNew.Data.Add("pointNo_SaveGeoCodeLocations", pointNo);
                throw exNew;
            }
        }

        //public void SaveGeoCodeLocations(string latitude, string longitude, string historyId, string dateTime, string pointno)
        //{
        //    try
        //    {
        //        string filename = "ActivityTracker/Location_" + historyId.ToString() + ".xml";
        //        if (!isoStore.DirectoryExists("ActivityTracker"))
        //            isoStore.CreateDirectory("ActivityTracker");

        //        if (isoStore.FileExists(filename))
        //        {
        //            isostream = new IsolatedStorageFileStream(filename, FileMode.Open, isoStore);
        //            xdoc = XDocument.Load(isostream);
        //            isostream.Close();

        //            var FindEle = (from f in xdoc.Descendants("Location")
        //                           select f);
        //            if (FindEle.Elements().Count() > 0)
        //            {
        //                xdoc.Root.Add(new XElement("point",
        //                            new XAttribute("lattitude", latitude),
        //                            new XAttribute("longitude", longitude),
        //                            new XAttribute("datetime", dateTime),
        //                            new XAttribute("pointno", pointno)));

        //                isostream = new IsolatedStorageFileStream(filename, FileMode.Create, isoStore);
        //                xdoc.Save(isostream);
        //                isostream.Close();
        //            }
        //        }
        //        else
        //        {

        //            isostream = new IsolatedStorageFileStream(filename, FileMode.Create, isoStore);
        //            xdoc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"),
        //                         new XElement("Location", new XAttribute("Id", historyId),
        //                        new XElement("point",
        //                            new XAttribute("lattitude", latitude),
        //                            new XAttribute("longitude", longitude),
        //                            new XAttribute("datetime", dateTime),
        //                            new XAttribute("pointno", pointno))));
        //            xdoc.Save(isostream);
        //            isostream.Close();

        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        Exception exNew = new Exception("Exception in SaveGeoCodeLocations Method In SaveData file\n\n" + ex.Message + " \n\n Stack Trace \n" + ex.StackTrace);
        //        exNew.Data.Add("historyIdinLocs", historyId);
        //        exNew.Data.Add("latitude", latitude);
        //        exNew.Data.Add("longitude", longitude);
        //        exNew.Data.Add("datetime", dateTime);
        //        throw exNew;
        //    }
        //}

        #endregion
    }
}
